package com.dooioo.pns.api.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * PackageName:com.dooioo.pns.api.controller
 * Author: Jerry.hu
 * Create: Jerry.hu (2015-07-20 上午10:07)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
public class BaseController {

	@ExceptionHandler(value = {IllegalArgumentException.class,IllegalStateException.class})
	protected ResponseEntity<?> showCode(Exception e){
		if(e instanceof IllegalArgumentException) {
			Map<String, String> map = new HashMap<>();
			map.put("code", "400");
			map.put("message", e.getMessage());
			return ResponseEntity.badRequest().body(map);
		}else{
			Map<String, String> map = new HashMap<>();
			map.put("code", "800");
			map.put("message", e.getMessage());
			return  ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(map);
		}
	}
}
